// order//order/receiveOrder/index.js
import getUserCard from "../../../utils/getUserCard.js";
const utils = require('../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oid: 0,
    createuid: 0,
    cardid: 0,
    orderInfo: {},
    proInfo: {},
    userInfo: {},
    screenHeight: 500,
    canUseOrder: false,
    mobile: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInfo(options.createuid);
    this.getUserInfo(options.fromuid);
    var screenHeight = wx.getSystemInfoSync().screenHeight;
    this.getOrderDetail(options.oid,options.fromuid);
    this.setData({
      oid: options.oid,
      createuid: options.createuid,
      screenHeight: screenHeight-80
    })
  },

  getOrderDetail: function(oid, uid){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Order/GetOrder?uid=' + uid + '&oid=' + oid,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data){
            let orderinfo = res.data.Data.OrderInfo;
            let proInfo = res.data.Data.OrderProductList[0].Info;
            orderinfo.expireTime = '';
            if(res.data.Data.OrderProductList[0].Extend && res.data.Data.OrderProductList[0].Extend.UseEndTime){
              orderinfo.expireTime = res.data.Data.OrderProductList[0].Extend.UseEndTime.substr(0,10);
            }
            if(proInfo.ShowImg.indexOf('ps') != -1){
              proInfo.ShowImg = utils.formatAvaterByProHead(proInfo.ShowImg);
            }else{
              proInfo.ShowImg = utils.formatAvater(proInfo.ShowImg,'https://static.711688.net.cn/upload/store/'+ app.globalData.storeId +'/product/editor/');
            }
            that.setData({
              orderInfo: orderinfo,
              proInfo: proInfo,
              canUseOrder: true
            })
          }
        }
      }
    })
  },
  toIndex: function(){
    wx.reLaunch({
      url: '/pages/home/index?isStartup=2',
    })
  },

  //获取商家信息
  getInfo: function(createUid) {
    var that = this;
    getUserCard(createUid, function (cardid) {
      wx.request({
        url: app.globalData.host + "/api/Staff/GetInfo?id=" + cardid,
        success: function(res) {
          if (res.data.Code == 0) {
            let slider = '';
            if(res.data.Data.IndustryCode > 0){//商家
              let sliders = res.data.Data.Slides;
              sliders = sliders.split(",");
              slider = sliders[sliders.length-1];
              slider = utils.formatAvater(slider, 'https://static.711688.net.cn/upload/card/source/');
            }else{//个人版
              slider = res.data.Data.Avater;
              slider = utils.formatAvater(slider, 'https://static.711688.net.cn/upload/card/source/');
            }
            that.setData({
              slider: slider,
              cardid: cardid,
              company: res.data.Data.Company
            })
          }
        },
      })
    })
    
  },

  getUserInfo: function(uid){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/User/GetPartUser?uid=' + uid,
      success: function(res){
        if(res.data.Code == 0){
          that.setData({
            userInfo: res.data.Data
          })
        }
      }
    })
  },

  receiveOrder: function(e){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Order/ReceiveTransferOrder?oid=' + that.data.oid + '&uid=' +　app.globalData.userInfo.Uid,
      success: function(res){
        if(res.data.Code == 0){
          wx.showToast({
            title: '接收成功',
            mask: true
          })
          wx.reLaunch({
            url: '/merchant/merchants/myCardCoupon/index?staffid=' + that.data.cardid + '&tagindex=4',
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(app.globalData.userInfo){
      if(app.globalData.userInfo.Mobile){
        that.setData({
          mobile: app.globalData.userInfo.Mobile
        })
      }
    }else{
      setTimeout(function () {
        this.onShow();
      }.bind(that), 1000)
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})